//
//  YLExchangeRecordViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/24.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLExchangeRecordViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLExchangeRecordModel.h"
@implementation YLExchangeRecordViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           return  [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               [WHProgressHUD showStatus:@"正在获取兑换记录..."];
               NSString *url = [NSString stringWithFormat:@"%@Point/PGetPointOrderHistory", YLURL];
               NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
               [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
               NSString *date = [dateFormatter stringFromDate:[NSDate date]];
               
               NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
               params[@"MonthCount"] = @(12);
               params[@"TimeIndex"] = date;
               [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                   
                   [WHProgressHUD dismiss];
                   
                   if ([responseObject[@"httpCode"] integerValue] == 200) {
    
                     weakSelf.group = [YLExchangeRecordGroup yy_modelWithDictionary:responseObject];
                       [subscriber sendNext:nil];
                   } else {
                       [WHProgressHUD showError:responseObject[@"message"]];
                   }
                   [subscriber sendCompleted];
               } fail:^(NSError *error) {
                   [subscriber sendCompleted];
               }];
               
                return nil;
            }];
        }];
    }
    return _requestCommand;
}

@end
